//定义学生对象
let student;
let oldPwdIsOk = false;
let newPwdIsOk = false;
let confirmPwdIsOk = false;

$(function (){
    //请求一次获取学生信息
    getStudent();
});

function getStudent(){
    let stuId = $("#stuId").val();
    $.ajax({
        type: "GET",
        url: "webapi/student/getById",
        data: {
            id:stuId
        }
    }).done(function (stuInfo) {
        console.log(stuInfo);
        student = stuInfo;
    })
}

function changePwd() {
    let newPwd = $("#newPwd").val();
    //三个输入都ok时
    if (oldPwdIsOk && newPwdIsOk && confirmPwdIsOk){
        $.ajax({
            type: "POST",
            url: "webapi/student/changePwd",
            data: {
                id: student.id,
                newPwd: newPwd
            }
        }).done(function (data) {
            if (data === 1){
                alert("修改成功");
                window.location.href = "/studentLogout";
            } else {
                alert("修改失败");
            }
        })
    } else {
        alert("输入有些许问题");
    }
}

function checkOldPwd() {
    let oldPwdInput = $("#oldPwd");
    let oldPwd = oldPwdInput.val();
    let realPwd = student.pwd;
    //console.log(realPwd);
    if (oldPwd !== realPwd){
        $("#warning1").show();
        oldPwdInput.focus();
        oldPwdIsOk = false;
    } else {
        $("#warning1").hide();
        oldPwdIsOk = true;
    }
}

function checkNewPwd() {
    let newPwdInput = $("#newPwd");
    let newPwd = newPwdInput.val();
    if (newPwd.length < 6){
        $("#warning2").show();
        newPwdInput.focus();
        newPwdIsOk = false;
    } else {
        $("#warning2").hide();
        newPwdIsOk = true;
    }
}

function checkConfirmPwd() {
    let confirmPwdInput = $("#confirmPwd");
    let confirmPwd = confirmPwdInput.val();
    let newPwd = $("#newPwd").val();
    if (confirmPwd !== newPwd){
        $("#warning3").show();
        confirmPwdInput.focus();
        confirmPwdIsOk = false;
    } else {
        $("#warning3").hide();
        confirmPwdIsOk = true;
    }
}